#include "logindialog.h"
#include "ui_logindialog.h"
#include <QMouseEvent>
#include <QTcpSocket>
#include <QDataStream>
#include <QSettings>
#include <QCryptographicHash>
#include "autologinthread.h"
#include "createuserdialog.h"
#include <QMessageBox>

LoginDialog::LoginDialog(QWidget *parent)
    : QDialog(parent)
    , ui(new Ui::LoginDialog)
{
    ui->setupUi(this);
    this->setAttribute(Qt::WA_DeleteOnClose);
    this->setWindowFlags(Qt::WindowStaysOnTopHint|Qt::FramelessWindowHint);
    QApplication::setOrganizationName("Assistant");
    this->setWindowTitle("登录界面");
    this->setWindowIcon(QIcon(":/res/icon/A.png"));
    readSettings();
}

LoginDialog::~LoginDialog()
{
    if (autoLoginThread) {
        autoLoginThread->disconnect();
        autoLoginThread->requestInterruption();  // 请求中断
        autoLoginThread->quit();                 // 退出事件循环
        autoLoginThread->wait();                 // 等待线程结束
        delete autoLoginThread;                  // 安全删除对象
    }
    delete ui;
}

QString LoginDialog::encrypt(const QString &str)
{
    QCryptographicHash hash(QCryptographicHash::Md5);//采用md5算法
    hash.addData(str.toLocal8Bit());
    return hash.result();
}

void LoginDialog::readSettings()
{
    QSettings settings;
    bool m_save=settings.value("save",false).toBool();
    bool m_autoLogin=settings.value("autoLogin",false).toBool();
    if(m_save){
        ui->chkSave->setChecked(true);
        ui->chekAutoLggin->setChecked(m_autoLogin);
        m_user=settings.value("user").toInt();
        m_pswd=settings.value("pswd").toString();
        ui->editUser->setText(QString::number(m_user));
        ui->editPSWD->setText(m_pswd);
        if(m_autoLogin){
            autoLoginThread=new AutoLoginThread(this);
            connect(autoLoginThread,&AutoLoginThread::TimeRun,this,&LoginDialog::do_autoLoginThread_TimeRun);
            autoLoginThread->start();
        }
    }else{
        ui->chkSave->setChecked(false);
    }
}

void LoginDialog::writeSetting()
{
    QSettings settings;
    m_save=ui->chkSave->isChecked();
    m_autoLogin=ui->chekAutoLggin->isChecked();
    m_user=ui->editUser->text().toInt();
    m_pswd=ui->editPSWD->text();
    settings.setValue("save",m_save);
    if(m_save){
        settings.setValue("autoLogin",m_autoLogin);
        settings.setValue("user",m_user);
        settings.setValue("pswd",m_pswd);
    }
}

void LoginDialog::mousePressEvent(QMouseEvent *event)
{
    if(event->button()==Qt::LeftButton){
        m_moving=true;
        QPoint mouseGlobalPos=event->globalPosition().toPoint();
        m_dPosion=mouseGlobalPos-this->pos();
    }
}

void LoginDialog::mouseMoveEvent(QMouseEvent *event)
{
    if(m_moving)
        this->move(event->globalPosition().toPoint()-m_dPosion);
}

//登录按键
void LoginDialog::on_btnLogin_clicked()
{
    mutex.tryLock();
    QHostAddress loginAddress(strAddress);
    loginSocket=new QTcpSocket(this);
    loginSocket->connectToHost(loginAddress,loginPort);
    qDebug()<<"connectToHost";
    connect(loginSocket,&QTcpSocket::connected,this,&LoginDialog::do_connected);
    connect(loginSocket,&QTcpSocket::readyRead,this,&LoginDialog::do_readyRead);
    mutex.unlock();
}


void LoginDialog::on_btnMiniSize_clicked()
{
    this->showMinimized();
}

//tcp成功连接成功，向服务器发送账号，密码
void LoginDialog::do_connected()
{
    QString user=ui->editUser->text();
    QString pswd=ui->editPSWD->text();
    QByteArray data="";
    QDataStream aStream(&data,QIODevice::WriteOnly);
    aStream<<user<<pswd;
    loginSocket->write(data);
}

//得到服务器账号密码反馈
void LoginDialog::do_readyRead()
{
    QByteArray data=loginSocket->readAll();
    QDataStream aStream(&data,QIODevice::ReadOnly);
    QString str;
    aStream>>str;
    qDebug()<<str;
    if(str=="Login"){
        writeSetting();
        this->accept();
    }else{
        loginSocket->disconnect();
        ui->editPSWD->clear();
        m_pswd="";
    }
}

void LoginDialog::do_autoLoginThread_TimeRun()
{
    if(ui->chekAutoLggin->isChecked()){
        ui->btnLogin->clicked();
    }
}

void LoginDialog::mouseReleaseEvent(QMouseEvent *event)
{
    m_moving=false;
}


void LoginDialog::on_btnCreate_clicked()
{
    createUserDialog=new CreateUserDialog(this);
    createUserDialog->exec();
    delete createUserDialog;
}

void LoginDialog::on_btnModifyPSWD_clicked()
{
    QMessageBox::information(this,"通知","找回密码还未实现，请等待版本更新！！！");
}


void LoginDialog::on_btnFindPSWD_clicked()
{
    QMessageBox::information(this,"通知","修改密码还未实现，请等待版本更新！！！");
}

